/*
**	Anderson Ferminiano
**	contato@andersonferminiano.com -- feel free to contact me for bugs or new implementations.
**	jQuery ScrollPagination
**	28th/March/2011
**	http://andersonferminiano.com/jqueryscrollpagination/
**	You may use this script for free, but keep my credits.
**	Thank you.
*/

(function( $ ){
	 
 var loading = false;  		 
 $.fn.scrollPagination = function(options) {
                //alert('init');
		var opts = $.extend($.fn.scrollPagination.defaults, options);  
		var target = opts.scrollTarget;
		if (target == null){
			target = obj; 
	 	}
		opts.scrollTarget = target;
                
		return this.each(function() {
		  $.fn.scrollPagination.init($(this), opts);
		});

  };
  
  $.fn.stopScrollPagination = function(){
	  return this.each(function() {
	 	$(this).attr('scrollPagination', 'disabled');
	  });
	  
  };
  
  $.fn.scrollPagination.loadContent = function(obj, opts){
         var curPage = opts.contentData.paged;
         var totalPages = opts.contentData.totalPages;
	 var target = opts.scrollTarget;
	 var mayLoadContent = $(target).scrollTop()+opts.heightOffset >= $(document).height() - $(target).height();
	 if (mayLoadContent && !loading){
                loading = true;
                if (curPage <= totalPages){
                    curPage += 1;
                    
                    if (opts.beforeLoad != null){
                           opts.beforeLoad(); 
                    }
                    $(obj).children().attr('rel', 'loaded');                 
                    $.ajax({
                             type: 'POST',
                             url: opts.contentPage,
                             data: opts.contentData,
                             success: function(data){  
                                var markup = '';
                                if (opts.dataType == "html")
                                    $(obj).append(data);  
                                else{
                                    //alert(data);
                                    if (opts.complete != null){
                                         markup = opts.complete(data);
                                         var newElements = jQuery(markup);
                                         newElements.appendTo(obj);
                                         //$(obj).append(result); 
                                    }
                                }
				var objectsRendered = $(obj).children('[rel!=loaded]');
				
				if (opts.afterLoad != null){
					opts.afterLoad(objectsRendered);	
				}
                                loading = false;
			  },
			  dataType: opts.dataType
                    });

                   opts.contentData.paged = curPage;
                }
	 }
	 
  };
  
  $.fn.scrollPagination.init = function(obj, opts){
	 var target = opts.scrollTarget;
	 $(obj).attr('scrollPagination', 'enabled');
	
	 $(target).scroll(function(event){
		if ($(obj).attr('scrollPagination') == 'enabled'){
	 		$.fn.scrollPagination.loadContent(obj, opts);		
		}
		else {
			event.stopPropagation();	
		}
	 });
	 
	 $.fn.scrollPagination.loadContent(obj, opts);
	 
 };
	
 $.fn.scrollPagination.defaults = {
      	'contentPage' : null,
     	'contentData' : {},
        'dataType' : 'json', //html
        'beforeLoad': null,
        'complete' : null,
        'afterLoad': null	,
        'scrollTarget': null,
        'heightOffset': 0		  
 };	
})( jQuery );